﻿using Day23Movies.Data;
using Day23Movies.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Day23Movies.Adapters.Data
{
    public class HomeDataAdapter : IHomeAdapter
    {
        public MovieListViewModel GetAllMovies()
        {
            MovieListViewModel model = new MovieListViewModel();

            using (MoviesContext db = new MoviesContext())
            {
                model.Movies = db.Movies.Select(m => new MovieViewModel()
                {
                    PosterUrl = m.PosterUrl,
                    Rating = m.Rating,
                    Title = m.Title,
                    UserName = m.User.UserName
                }).ToList();
            }

            model.AverageRating = model.Movies.Average(m => m.Rating);

            return model;
        }
    }
}